package com.sghd.football.robot.action;

import com.sghd.common.socket.client.Client;
import com.sghd.common.socket.client.SocketCallbackTemplate;
import com.sghd.common.socket.core.Message;
import com.sghd.football.module.game.facade.model.ConneReq;
import com.sghd.football.robot.core.Robot;
import com.sghd.football.utils.SocketEnum;

/**
 * @author wch
 * @date 2018-07-06 16:57
 * @desc 连接游戏服务器
 **/
public class ConnAction extends AbstractAction {

    @Override
    public Stage stage() {
        return Stage.CONN;
    }

    @Override
    public void action(Robot robot) {
        if (!check(robot)) {
            return;
        }
        Client client = robot.getClient();
        ConneReq req = new ConneReq();
        req.setPlayerId(robot.getRobotId());
        req.setRoomId(0);
        client.getSession().getContext().setIdentity(req.getPlayerId());//设置机器人标识
        Message message = Message.valueOf(SocketEnum.CONN, req);
        client.send(message, new SocketCallbackTemplate() {
            @Override
            public void onSuccess(Message response) {
                robot.setNext(new EnterAction());
            }
        });
    }
}
